<?php
class Cp_PageController extends Pidguin_Controller_Cp_Action
{
	public function indexAction()
	{
		$rowset = Model_Page::getInstance()->fetchAll();
		$this->view->pages = $rowset;
	}
	
	public function deleteAction()
	{		
		if (!$this->_getParam('id')) {
			throw new Zend_Controller_Action_Exception('Page id is not set');
		}
		
		$m = Model_Page::getInstance();
		$select = $m->select()->where(Model_Page::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$row = $m->fetchRow($select);
		if (!$row) {
			throw new Zend_Controller_Action_Exception('Page not found');
		}
		$row->delete();
		$this->_redirect('/cp/page');
	}
	
	public function editAction()
	{
	    if (!$this->_getParam('id')) {
	        throw new Zend_Controller_Action_Exception('Page ID is not set');
	    }
	    
	    $m = Model_Page::getInstance();
	    $select = $m->select()->where(Model_Page::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
	    $row = $m->fetchRow($select);
	    
	    if (!$row) {
	        throw new Zend_Controller_Action_Exception('Page not found');
	    }
	    
	    $_attachments = $row->findDependentRowset('Model_AttachmentsForPages');
	    $attachments = array();
	    if (count($_attachments)) {
	        foreach ($_attachments as $attachment) {
	            $_a = $attachment->findParentRow('Model_Attachments');
	            $attachments[] = array('url' => $_a->url, 'id' => $_a->id, 'type' => $_a->type);
	        }
	    }
	    $this->view->page = $row;
	    $this->view->attachments = Zend_Json::encode($attachments);
	}
	
	public function saveAction()
	{
		if (!$this->getRequest()->isXmlHttpRequest()) {
			throw new Zend_Controller_Action_Exception('Not ajax');
		}
		
		if (!$this->_getParam('id')) {
	        throw new Zend_Controller_Action_Exception('Page ID is not set');
	    }
		
		if (!$this->_getParam('title')) {
			throw new Zend_Controller_Action_Exception('Title is not set');
		}
				
		if (!preg_match('/[a-zA-Zа-яА-Я0-9_\s\-\.\+\=\(\)\;\:\'\"\?\!\%\$\#\@]*/i', $this->_getParam('title'))) {
			throw new Zend_Controller_Action_Exception('Title incorrect');
		}
		
		if (!$this->_getParam('url')) {
			throw new Zend_Controller_Action_Exception('Url is not set');
		}
		
		if (!preg_match('/[a-zA-Z0-9_\-\.]*/i', $this->_getParam('url'))) {
			throw new Zend_Controller_Action_Exception('Url incorrect');
		}
		
		if (!$this->_getParam('text')) {
			throw new Zend_Controller_Action_Exception('Text is not set');
		}
		
		$pm = Model_Page::getInstance();
		$ps = $pm->select()->where(Model_Page::getDefaultAdapter()->quoteInto('id = ?', $this->_getParam('id')));
		$pr = $pm->fetchRow($ps);
		
		if (!$pr) {
		    throw new Zend_Controller_Action_Exception('Page not found');
		}
		$pr->title = $this->_getParam('title');
		$pr->name = $this->_getParam('url');
		$pr->url = '/page/' . $this->_getParam('url');
		$pr->text = $this->_getParam('text');
		$pr->author_id = Pidguin_Client::getInstance()->getClient()->user->id;
		$pr->modified = date('Y-m-d H:i:s');
		$pr->save();
		
		$attachs = $this->_getParam('attachments');
		if (count($attachs) > 0) {
			$_am = Model_Attachments::getInstance();
			foreach ($attachs as $attach) {
				if (!preg_match('/^[0-9]*$/', $attach)) {
					continue;
				}
				$_ar = $_am->fetchRow($_am->select()->where('id = ?', $attach));
				if (!$_ar) {
					continue;
				}
				$j = $_ar->findDependentRowset('Model_AttachmentsForPages', 'Attachment');
				if (count($j) > 0) continue;
				$afpr = Model_AttachmentsForPages::getInstance()->createRow();
				$afpr->page_id = $pr->id;
				$afpr->attachment_id = $attach;
				$afpr->save();
			}
		}
		$this->_helper->json(array('status' => 'ok'));
	}
	
	public function createAction()
	{
		if (!$this->getRequest()->isXmlHttpRequest()) {
			throw new Zend_Controller_Action_Exception('Not ajax');
		}
		
		if (!$this->_getParam('title')) {
			throw new Zend_Controller_Action_Exception('Title is not set');
		}
				
		if (!preg_match('/[a-zA-Zа-яА-Я0-9_\s\-\.\+\=\(\)\;\:\'\"\?\!\%\$\#\@]*/i', $this->_getParam('title'))) {
			throw new Zend_Controller_Action_Exception('Title incorrect');
		}
		
		if (!$this->_getParam('url')) {
			throw new Zend_Controller_Action_Exception('Url is not set');
		}
		
		if (!preg_match('/[a-zA-Z0-9_\-\.]*/i', $this->_getParam('url'))) {
			throw new Zend_Controller_Action_Exception('Url incorrect');
		}
		
		if (!$this->_getParam('text')) {
			throw new Zend_Controller_Action_Exception('Text is not set');
		}
		$result = array();
		$pr = Model_Page::getInstance()->createRow();
		$pr->title = $this->_getParam('title');
		$pr->name = $this->_getParam('url');
		$pr->url = '/page/' . $this->_getParam('url');
		$pr->text = $this->_getParam('text');
		$pr->author_id = Pidguin_Client::getInstance()->getClient()->user->id;
		$pr->created = date('Y-m-d H:i:s');
		$pr->save();
		
		$result['id'] = $pr->id;
		$result['title'] = $pr->title;
		$result['url'] = $pr->url;
		$result['author'] = $pr->findParentRow('Model_User')->name;
		$date = new Zend_Date($pr->created, false, Pidguin_Application::getInstance()->getLocale());
		$result['created'] = $date->toString('dd MMMM YYYYг.');
		
		$attachs = $this->_getParam('attachments');
		if (count($attachs) > 0) {
			$_am = Model_Attachments::getInstance();
			foreach ($attachs as $attach) {
				if (!preg_match('/^[0-9]*$/', $attach)) {
					continue;
				}
				$_ar = $_am->fetchRow($_am->select()->where('id = ?', $attach));
				if (!$_ar) {
					continue;
				}
				$afpr = Model_AttachmentsForPages::getInstance()->createRow();
				$afpr->page_id = $pr->id;
				$afpr->attachment_id = $attach;
				$afpr->save();
				$result['attachs'][] = array('id' => $_ar->id, 'url' => $_ar->url);
			}
		}
		$this->_helper->json($result);
	}
}
